import type Typings from "@/typings";
import { ref } from "vue";
import { defineStore } from "pinia";
import service from "@/apis";

const useMenuStore = defineStore(
  "menuStore",
  () => {
    // 设置默认值才能被 $reset 重置
    const menuList = ref<Partial<Typings.Menu.props>[]>([]);

    // 获取菜单数据
    const getMenuList = async () => {
      if (menuList.value.length) return menuList.value;
      const { data } = await service.menuTreeApi({ systemName: "CONTROL" });
      menuList.value = data || [];
    };

    return {
      menuList,
      getMenuList,
    };
  },
  {
    persist: {
      enabled: true,

      strategies: [
        {
          storage: sessionStorage,
          paths: ["menuList"],
        },
      ],
    },
  } as any
);

export default useMenuStore;
